package com.eastfair.transaction.util;

import com.alibaba.fastjson.JSONObject;
import com.eastfair.core.utils.HeaderUtil;
import com.eastfair.log.point.PointUtil;
import com.eastfair.mq.model.MySimpleMessage;
import com.eastfair.mq.util.MessageKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

@Component
public class TransactionUtil {

    @Autowired
    PointUtil pointUtil;


    /**
     * 获取埋点名称
     *
     * @param object
     * @return
     */
    public String getSendPointNameInvok(Object object) {
        Method m = null;
        try {
            m = object.getClass().getMethod("getSendPointName");
            Object pointName = m.invoke(object);
            if (object == null) {
                return null;
            }
            return pointName.toString();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 获取埋点名称
     *
     * @param object
     * @return
     */
    public String getReceivePointNameInvok(Object object) {
        Method m = null;
        try {
            m = object.getClass().getMethod("getReceivePointName");
            Object pointName = m.invoke(object);
            if (object == null) {
                return null;
            }
            return pointName.toString();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return null;
    }


    /**
     * 获取埋点名称
     *
     * @param businObject
     * @return
     */
    public MySimpleMessage assembleSimpleMessage(Object businObject) {
        /********提交订单********/
        //组装头消息
        Map<String, Object> headers = HeaderUtil.getHeaderMap();
        String key = MessageKeyUtil.assembleKey();
        headers.put("key", key);
        headers.put("sender", getSendPointNameInvok(businObject));
        headers.put("tag", getReceivePointNameInvok(businObject));

        //组装发送体
        MySimpleMessage mySimpleMessage = new MySimpleMessage();
        mySimpleMessage.setHeaders(headers);
        mySimpleMessage.setPayload(businObject);
        return mySimpleMessage;
    }

    /**
     * 获取发送方
     *
     * @param mySimpleMessage
     * @return
     */
    public String querySender(MySimpleMessage mySimpleMessage) {
        Map<String, Object> headers = mySimpleMessage.getHeaders();
        //获取发送方
        String sender = headers.get("sender").toString();
        return sender;
    }


    /**
     * 获取tag
     *
     * @param mySimpleMessage
     * @return
     */
    public String queryTag(MySimpleMessage mySimpleMessage) {
        Map<String, Object> headers = mySimpleMessage.getHeaders();
        String tag = headers.get("tag").toString();
        return tag;
    }
}
